//
//  CustomTableCell.m
//  GridViewTest1
//
//  Created by Eidan Lin on 12-12-17.
//  Copyright (c) 2012年 taomm. All rights reserved.
//

#import "CustomTableCell.h"
#import <QuartzCore/QuartzCore.h>
#import "UIView+EidanSize.h"
#import "CustomGridView.h"

@implementation CustomTableCell

-(id) initWithIdentifier:(NSString *)identifier{
    if (self = [super init]) {
        [self setUpWithIdentifier:identifier];
    }
    return self;
}

-(void)setUpWithIdentifier:(NSString *)identifier{
    
    self.reuseIdentifier=identifier;
    self.clipsToBounds=YES;
    
    [[NSBundle mainBundle] loadNibNamed:identifier owner:self options:nil];
    self.width=self.customView.width;
    self.height=self.customView.height;
    [self addSubview:self.customView];

//    self.containerView=(UIView *)[self.customView viewWithTag:300];
//    self.fakeImageView=(UIImageView *)[self.customView viewWithTag:301];
//    self.trueImageView=(UIImageView *)[self.customView viewWithTag:302];
//    self.tilteLabel=(UILabel *)[self.customView viewWithTag:200];
//    self.infoLabel=(UILabel *)[self.customView viewWithTag:202];
//    self.clickBtn=(UIButton *)[self.customView viewWithTag:201];
//    self.priseBtn = (UIButton*)[self.customView viewWithTag:400];
//    self.clickBtn.exclusiveTouch=YES;  //防止同时点击
    
    [self handleTheSelectedBtnInFirstSetUp];
}

//self的frame改变的时候，会调用这句话，这个时候，你就来改变custom里面的东西了。。
- (void)layoutSubviews{
    //同时也要让图片大小跟着变，可以设置xib中图片的AutoSizing
    self.customView.width=self.width;
    self.customView.height=self.height;
}

//---------------------------------------------------------编辑模式 start---------------

//编辑模式的选择按钮处理。。
-(void)handleTheSelectedBtnInFirstSetUp{
    
    self.isInEditingMode = NO;
    self.selectedBtnBgView = [self.customView viewWithTag:1000];
    self.selectedBtnBgView.hidden = YES;
    
    self.selectedBtn=(UIButton *)[self.customView viewWithTag:1001];
    self.selectedBtn.exclusiveTouch = YES;
    self.selectedBtn.hidden = YES;
    [self.selectedBtn addTarget:self action:@selector(selectedBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
}

//每个cell进入和退出编辑模式
-(void)customTableCellSetEdit:(BOOL)edit andAnimation:(BOOL)animation{
    float animationDur=.3;
    if (!animation) {
        animationDur=0;
    }
    
    self.selectedBtn.selected = NO;
    if (edit) {
        self.selectedBtnBgView.hidden = NO;
        self.selectedBtn.hidden = NO;
    }else{
        self.selectedBtnBgView.hidden = YES;
        self.selectedBtn.hidden = YES;
    }
    
    self.isInEditingMode=edit;
}

-(void)handleIsInEditingModeWithParent:(CustomGridView *)parent andIsInEditing:(BOOL) edit{
    self.parent = parent;
    self.isInEditingMode = edit;
    self.index = self.indexPath.row * self.parent.numsOfColumn + self.indexPath.column;
    
    //是否在编辑模式
    if (edit) {
        self.selectedBtnBgView.hidden = NO;
        self.selectedBtn.hidden = NO;
    }else{
        self.selectedBtnBgView.hidden = YES;
        self.selectedBtn.hidden = YES;
    }
    
    self.selectedBtn.selected=NO;
    if (self.parent && self.isInEditingMode) {  //是否在数组里面。。
        if ([self.parent theCellIsInTheHasSelectedArray:self.index]) {
            self.selectedBtn.selected=YES;
        }
    }
    
}

-(void)selectedBtnClicked:(id)sender{
    UIButton *btn=(UIButton *)sender;
    
    if (btn.selected) {  //点击后不选择
        btn.selected=NO;
        [self.parent.hasSelectedArray removeObject:[NSNumber numberWithInt:self.index]];
    }else{  //点击后选择
        btn.selected=YES;
        [self.parent.hasSelectedArray addObject:[NSNumber numberWithInt:self.index]];
    }
    [self selectedBtnClickedCallBack];
    
}

-(void)selectedBtnClickedCallBack{
    if ([self.delegate respondsToSelector:@selector(customGridViewTableCellInEditingModeAndSelectedBtnHasClicked:)]) {
        [self.delegate customGridViewTableCellInEditingModeAndSelectedBtnHasClicked:self];
    }
}


//---------------------------------------------------------编辑模式 end-----------------------------------------------------

@end


@implementation CustomIndexPath

+ (CustomIndexPath *)indexPathForRow:(NSInteger)row inColumn:(NSInteger)column{
    CustomIndexPath *indexPath=[[CustomIndexPath alloc] init];
    indexPath.row=row;
    indexPath.column=column;
    return indexPath;
}

@end
